import math

from scipy.optimize import fsolve
import 离散点选择 as selectLiSan
import 获得所有碰撞的板 as getCrashBoard
import step as step


def getPreJiao(x0, y0, len, cjiao, a):
    p = 0.01
    flen = len
    fx = x0
    fy = y0

    def qaulityJiao(jiao):
        x1 = jiao * a * math.cos(jiao)
        y1 = jiao * a * math.sin(jiao)
        q1 = (y1 - fy) ** 2 + (x1 - fx) ** 2 - flen ** 2
        return q1

    preJiao = fsolve(qaulityJiao, cjiao - p)
    while preJiao[0] > cjiao or math.fabs(a * cjiao - a * preJiao[0]) > 0.55 / 2:
        p = p + 0.1
        preJiao = fsolve(qaulityJiao, cjiao - p)
    return preJiao[0]


def getLuoJu():
    tempDots = []
    tempJiaos = []
    jiaos, dots = step.getJiaoAndDots()
    for i in range(len(jiaos)):
        tempJiaos.append(jiaos[i])
        tempDots.append([dots[i][0], dots[i][1]])
    left = int(0.55 * 10 ** 6)
    right = int(1.7 * 10 ** 6)

    while left <= right:
        mid = ((left + right) / 2)
        a = mid / (2 * math.pi) / 10 ** 6
        flag = 0
        while math.sqrt(dots[0][0] ** 2 + dots[0][1] ** 2) >= 9 / 2:
            preJiao1 = getPreJiao(dots[1][0], dots[1][1], 1.65, jiaos[1], a)
            prex = preJiao1 * a * math.cos(preJiao1)
            prey = preJiao1 * a * math.sin(preJiao1)
            touJiao = getPreJiao(prex, prey, 2.86, preJiao1, a)
            toux = touJiao * a * math.cos(touJiao)
            touy = touJiao * a * math.sin(touJiao)
            for i in range(224 - 3, -1, -1):
                jiaos[i + 2] = jiaos[i + 1]
                dots[i + 2][0] = dots[i + 1][0]
                dots[i + 2][1] = dots[i + 1][1]
            jiaos[0] = touJiao
            jiaos[1] = preJiao1
            dots[0][0] = toux
            dots[0][1] = touy
            dots[1][0] = prex
            dots[1][1] = prey
            resultIndexsTou = selectLiSan.getLiSanDots(jiaos, 0)
            crashBoardsTou = getCrashBoard.getCrashBoard(dots, resultIndexsTou, 0)
            resultIndexs1 = selectLiSan.getLiSanDots(jiaos, 1)
            crashBoards1 = getCrashBoard.getCrashBoard(dots, resultIndexs1, 1)
            if len(crashBoards1) + len(crashBoardsTou) > 0:
                flag = 1
                break
        for i in range(len(jiaos)):
            jiaos[i] = tempJiaos[i]
            dots[i][0] = tempDots[i][0]
            dots[i][1] = tempDots[i][1]
        if flag == 1:
            left = mid + 1
        else:
            right = mid - 1

    left /= 10 ** 6
    return left


print(getLuoJu())
